﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using ExamSystem.Mdeol;
namespace ExamSystem.DAL
{
    public class TanswerDAL
    {
        //根据试卷id和学生id查询所有试题信息
        public List<Tanswer> querybypageridandstuid(int tpid,int tsid)
        {
            List<Tanswer> list = new List<Tanswer>();
            string sql = " select a.*,b.taanswer from (select * from tquestion where tqId in(select tqId from tpagerquestion where tpId=" + tpid + ")) a left join (select * from tanswer where tpId=" + tpid + " and tsId=" + tsid + ") b on a.tqId=b.tqId order by a.tqId";
            DataSet ds = DBhepter.GetDataSet(sql);
            DataTable td = ds.Tables[0];
            for (int i = 0; i < td.Rows.Count;i++ )
            {
                DataRow dr = td.Rows[i];
                int tqId = int.Parse(dr["tqId"].ToString());
                string tqName = dr["tqName"].ToString();
                string tqtype = dr["tqtype"].ToString();
                float tqScore = float.Parse(dr["tqScore"].ToString());
                string tqA = dr["tqA"].ToString();
                string tqB = dr["tqB"].ToString();
                string tqC = dr["tqC"].ToString();
                string tqD = dr["tqD"].ToString();
                string tqAnswer = dr["tqAnswer"].ToString();
                string taanswer = dr["taanswer"].ToString();
                Tquestion tq = new Tquestion(tqId, null, tqName, tqtype, tqScore, tqA, tqB, tqC, tqD, tqAnswer, taanswer);
                Tanswer t = new Tanswer(0,null,tq,null,taanswer);
                list.Add(t);
            }
            return list;
        }



        //修改答案
        public bool updatastuanswer(int tpid,int tqid,int tsid,string taanswer)
        {
            string sql1 = "delete from tanswer where tpid="+tpid+" and tqid="+tqid+" and tsid="+tsid+"";
            string sql2 = "insert into tanswer values("+tpid+","+tqid+","+tsid+",'"+taanswer+"')";
            List<string> list = new List<string>();
            list.Add(sql1);
            list.Add(sql2);
            return DBhepter.ExecuteTransaction(list);
        }

    }
}